﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ObjectPersistence;
using BusinessLayerHelper;
using KYO.DAL;

public partial class Admin_Controls_Mod_AddinManager_AddinManager : System.Web.UI.UserControl, IAddinManager
{
    TabAssembler tabService = new TabAssembler();
    ModuleAssembler moduleService = new ModuleAssembler();
    protected void Page_Load(object sender, EventArgs e)
    {
       
    }
    protected override void OnInit(EventArgs e)
    {
       
            LoadData();
       
        base.OnInit(e);
    }

    void LoadData()
    {
        //load data to Tab LIst
        lb_Tab.DataSource = tabService.ReadCollectionAll();
        lb_Tab.DataTextField = "Name";
        lb_Tab.DataValueField = "TabId";
        lb_Tab.DataBind();

       
        //LOad Data;
        LoadPositionData();
        
    }

    void LoadPositionData()
    {
         Array obj = Enum.GetValues(typeof(Position));
         foreach (Position p in obj)
         {
             lb_Position.Items.Add(new ListItem(p+"", (int)p+""));
         }


    }

    #region ControlProperty
    public string ModuleTitle
    {
        set 
        {
            tb_Title.Text = value;
        }
        get 
        {
            return tb_Title.Text;
        }
    }
    public bool ModuleVisible
    {
        set
        {
            cb_Visible.Checked = value;
        }
        get
        {
            return cb_Visible.Checked;
        }
    }
    public Position ModulePosition
    {
        set
        {
            lb_Position.SelectedValue = ((int)value).ToString();
        }
        get
        {
            return (Position)(int.Parse(lb_Position.SelectedValue));
        }
    }
    public int ModudeOdering
    {
        set
        {
            tb_Odering.Text = value+"";
        }
        get
        {
            return int.Parse(tb_Odering.Text);
        }
    }
    public PersistentList<Tab> Tabs
    {

        set
        {
             PersistentList<Tab> listTabs = value;
             List<int> list = new List<int>();
             foreach (Tab t in listTabs)
             {
                 list.Add(t.TabId);
 
             }

            for (int i = 0; i < lb_Tab.Items.Count; i++)
            {
                if (list.Contains(int.Parse(lb_Tab.Items[i].Value)))
                {
                    lb_Tab.Items[i].Selected= true;
                }
                  
            }

        }

        get
        {
            PersistentList<Tab> listTabs = new PersistentList<Tab>();
            for (int i = 0; i < lb_Tab.Items.Count; i++)
            {
                if (lb_Tab.Items[i].Selected)
                { 
                    //Get Tabid
                    Tab tab = tabService.ReadSingleByKey(int.Parse(lb_Tab.Items[i].Value));
                    tab.IsNew = false;
                    listTabs.Add(tab);
                }
                
            }
            return listTabs;
        }

    }
    #endregion
}
